home *** CD-ROM | disk | FTP | other *** search
- Path: newsserver.trl.OZ.AU!usenet
- From: t.hand@trl.oz.au (Trevor Hand)
- Newsgroups: comp.lang.c++
- Subject: Re: Need help on "string" conditional statements
- Date: 9 Jan 1996 23:02:55 GMT
- Organization: Telstra Research Laboratories
- Message-ID: <4cus6v$383@newsserver.trl.OZ.AU>
- References: <4beid0$h0e@news.ios.com> <30EF60C5.58CB@www.luminaire.com>
- NNTP-Posting-Host: tmp_ip_159.trl.oz.au
- X-Newsreader: News Xpress Version 1.0 Beta #3
-
- In article <30EF60C5.58CB@www.luminaire.com>,
- "Edwin P. Jacques" <pierre@www.luminaire.com> wrote:
- >Keith Boruff wrote:
- >>
- >> I am trying to enter a string in a program and check this string with a
- >> conditional "if" statement:
- >>
- >> char StringName[3];
- >>
- >> cout << "Enter string";
- >> cin >> StringName; // I enter "Jan" without quote marks
- >>
- >> if (StringName == "Jan")
- >
- If you want to compare StringName to an array of characters try using strcmp
- eg. if (strcmp(StringName,"Jan")) If the two strings are equal ie. the
- same, strcmp will return a 0, which is of course "false". To get over the
- problem and make the result appear to the programmer to be what is expected I
- would change the statement to -
-
- if (!(strcmp(StringName,"Jan")))
- //Statements if the same
- else
- //Statements if different
-
- If you want to make a case insensitive comparison use stricmp in place of
- strcmp. I had the same problems at first too, I guess it's my BASIC
- background!!
-
- Usually I use the string class though, it makes life a LOT easier.
-
- Trevor Hand
- t.hand@trl.oz.au
-